<?php

class CategoryController extends Controller {

    public function actionIndex($id = null) {
        if ($id != null && $id != 1) {
            $model = Category::model()->findByPk($id);
            if ($model != null) {
                //add header
                $this->pageTitle = $model->getSeoTitle();
                $model->registerMeta();
                //
                $descendants = $model->ancestors()->findAll();
                foreach($descendants as $item){
                   if($item->id != 1){
                       $this->breadcrumbs[] = array(
                            'title' => $item->name,
                            'link' => $item->getUrl()
                        ); 
                   }
                }
                $this->breadcrumbs[] = array(
                        'title' => $model->name,
                        'link' => $model->getUrl()
                );
                //gt product
                $sort = isset($_POST['sort']) ? $_POST['sort'] : "view DESC";
                $products = $model->getAllProduct($sort);
                $this->render('index', array('category' => $model, 'products' => $products, 'sort' => $sort));
            } else {
                throw new CHttpException(404, 'The category cannot be found.');
            }
        } else {
            throw new CHttpException(404, 'The category cannot be found.');
        }
    }

    /**
     * 
     */
    public function actionMore() {
        $this->layout = true;
        $content = "";
        $id = Yii::app()->request->getQuery('id',0);
        $offset = Yii::app()->request->getQuery('offset',0);
        //$offset = $offset > 0?$offset -1:$offset;
        $sort = Yii::app()->request->getQuery('sort',"view DESC");
        $model = Category::model()->findByPk($id);
        if ($model != null) {
            $products = $model->getAllProduct($sort,$offset,LIMIT);
            $content = $this->render('more', array('products' => $products),true);
        }
        echo $content;exit();
    }

}
